﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAL
{
    public class BankBookDAL:Base<BankBook>
    {
        public BankBookDAL() : base("BankBook", "ID") { }

        public override bool Insert(BankBook entity)
        {
            string sql = "insert into `bankbook` (`AID`,`Card_ID`,`Rate`,`Capital`,`Cycle`,`Total`,`Save_Time`) values(@aid,@cid,@r,@c,@cyc,@t,@st)";
            var r = MySQLHelper.ExecuteNonQuery(sql,GetParams("aid",entity.AID),
                GetParams("cid", entity.CardID),
                GetParams("r", entity.Rate),
                GetParams("c", entity.Capital),
                GetParams("cyc", entity.Cycle),
                GetParams("t", entity.Total),
                GetParams("st", entity.SaveTime)) == 1;
            if (r)
            {
                entity.ID = GetLastInsertID();
                return true;
            }
            return false;
        }

        public override bool Update(BankBook entity)
        {
            throw new NotImplementedException();
        }

        public override BankBook GetEntityById(int id)
        {
            throw new NotImplementedException();
        }

        public override BankBook GetEntityById(string id)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取用户的存折记录
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public List<BankBook> GetListByAID(int aid)
        {
            var table = MySQLHelper.Query("select * from bankbook where aid=" + aid);
            List<BankBook> list = new List<BankBook>();
            foreach (DataRow row in table.Rows)
            {
                BankBook bb = new BankBook()
                {
                    AID = aid,
                    ID = int.Parse(row["ID"].ToString()),
                    CardID = int.Parse(row["card_id"].ToString()),
                    Cycle = int.Parse(row["Cycle"].ToString()),
                    Capital = decimal.Parse(row["Capital"].ToString()),
                    Rate = float.Parse(row["Rate"].ToString()),
                    SaveTime = DateTime.Parse(row["Save_Time"].ToString()),
                    Total = decimal.Parse(row["Total"].ToString())
                };
                list.Add(bb);
            }
            return list;
        }
    }
}
